package com.blogs.web.interceptor;

import com.blogs.common.constant.Types;
import com.blogs.common.constant.WebConst;
import com.blogs.common.utils.AdminCommons;
import com.blogs.common.utils.Commons;
import com.blogs.domain.db.Options;
import com.blogs.domain.dto.MetaDto;
import com.blogs.domain.dto.StatisticsDto;
import com.blogs.service.meta.MetaService;
import com.blogs.service.option.OptionService;
import com.blogs.service.site.SiteService;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义拦截器
 * @author hubiao
 */
@Component
public class BaseInterceptor implements HandlerInterceptor {
    @Resource
    private OptionService optionService;

    @Resource
    private Commons commons;

    @Resource
    private AdminCommons adminCommons;

    @Resource
    private MetaService metaService;

    @Resource
    private SiteService siteService;

    @Resource
    private HttpSession session;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView view) {
        Options ov = optionService.getOptionByName( "site_record" );
        // 分类总数
        Long categoryCount = metaService.getMetasCountByType( Types.CATEGORY.getType() );
        // 标签总数
        Long tagCount = metaService.getMetasCountByType( Types.TAG.getType() );
        // 获取文章总数
        StatisticsDto statistics = siteService.getStatistics();
        // 获取友情链接
        List<MetaDto> links = metaService.getMetaList( Types.LINK.getType(), null, WebConst.MAX_POSTS );

        session.setAttribute( "categoryCount", categoryCount );
        session.setAttribute( "tagCount", tagCount );
        session.setAttribute( "articleCount", statistics.getArticles() );
        session.setAttribute( "links", links );
        request.setAttribute( "commons", commons );
        request.setAttribute( "option", ov );
        request.setAttribute( "adminCommons", adminCommons );
        request.setAttribute( "title", "" );
        initSiteConfig( request );
    }

    private void initSiteConfig(HttpServletRequest request) {
        if (WebConst.initConfig.isEmpty()) {
            List<Options> options = optionService.getOptions();
            Map<String, String> querys = new HashMap<>();
            options.forEach( option -> {
                querys.put( option.getName(), option.getValue() );
            } );
            WebConst.initConfig = querys;
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
    }
}
